package fr.enseirbmatmeca.apms.tapestry.pages.account;

import javax.inject.Inject;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Service;

import fr.enseirbmatmeca.apms.spring.model.user.User;
import fr.enseirbmatmeca.apms.spring.service.UserManager;
import fr.enseirbmatmeca.apms.tapestry.entities.Account;

public class CreateAccount {

	private static final String BAD_CREDENTIALS = "Bad accountin and/or password. Please retry.";

	@Property
	@Persist(PersistenceConstants.FLASH)
	private String message;

	@Inject
	private UserManager userManager;

	@Property
	private Account account;

	@Property
	private User user;

	@InjectPage
	private Home home;

	Object onSuccess() {
		/*
		 * if (userManager.checkLogin(account.identifiant, account.motDePasse))
		 * { home.setup(account); return home; } message = BAD_CREDENTIALS;
		 * return null;
		 */
		user = userManager.createUser(account.identifiant, account.motDePasse);
		if (userManager.saveUser(user)) {
			home.setup(account);
			return home;
		}
		message = BAD_CREDENTIALS;
		return null;
	}

}
